#pragma once
#include <iostream>
#include <string>
#include <ctemplate/template.h>
#include "oj_model.hpp"
namespace ns_view
{
    using namespace ns_model;
    class view
    {
    public:
        view() {}
        ~view() {}
        const std::string template_path = "./template_html/";
        void allqexpand(const std::vector<question> &questions, std::string *html)
        {
            //LOG(DEBUG)<<"开始渲染"<<std::endl;
            // 1.形成路径
            //LOG(DEBUG)<<"1.形成路径"<<std::endl;
            std::string src_html = template_path + "allquestion.html";
            // 2.形成数字典
            //LOG(DEBUG)<<"2.形成数字典"<<std::endl;
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }
            // 3. 获取被渲染的html
            //LOG(DEBUG)<<"3.获取被渲染的html"<<std::endl;
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 开始完成渲染功能
            //LOG(DEBUG)<<"4.开始完成渲染功能"<<std::endl;
            tpl->Expand(html, &root);

        }
        void oneqexpand(const question &question, std::string *html)
        {
            // 1. 形成路径
            std::string src_html = template_path + "onequestion.html";

            // 2. 形成数字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", question.number);
            root.SetValue("title", question.title);
            root.SetValue("star", question.star);
            root.SetValue("desc", question.desc);
            root.SetValue("pre_code", question.header);

            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 开始完成渲染功能
            tpl->Expand(html, &root);
        }
    };

}